import java.util.concurrent.atomic.AtomicInteger;

public class Demo1 {
    //原子类就不用加锁了,通过方法替代操作符
    private static AtomicInteger count = new AtomicInteger(0);//定义一个原子类对象,初始化为0
    //此处是没有用到任何的加锁操作的
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i <50000 ; i++) {
                //count++
                count.getAndIncrement();
                //++count
                count.incrementAndGet();
                //--count
                count.decrementAndGet();
                //count+=10
                count.addAndGet(10);
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                //count++
                count.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count = "+count);
    }
}
